package com.ruoyi.web.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.config.DbInterfaceConfig;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.CommonOKNOEnum;
import com.ruoyi.common.enums.FlightPathTypeEnum;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.crudinterface.CrudInterfaceUtils;
import com.ruoyi.common.utils.crudinterface.vo.ReqVo;
import com.ruoyi.web.domain.LsKyJssyb;
import com.ruoyi.web.utils.IdUtils;
import com.ruoyi.web.utils.RemoveDuplicateCharacters;
import com.ruoyi.web.utils.TranslateNewUtils;
import com.ruoyi.web.vo.SearchVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class LsKyJssybService {

    @Value("${translateUrl}")
    private String translateUrl;
//    @Resource
//    private LsKyJssybMapper lsKyJssybMapper;
    @Autowired
    private DbInterfaceConfig dbInterfaceConfig;

    public AjaxResult identifyTranslate(String keyword) {
        log.info("翻译工具-自动识别语言 keyword = {}", keyword);
        CrudInterfaceUtils crudInterfaceUtils = new CrudInterfaceUtils();
        StringBuilder sql = new StringBuilder();
        sql.append("SELECT ID, MC, JG FROM J_KY_S_JSSYB ORDER BY ID ");
        Object dbList = crudInterfaceUtils.getList(dbInterfaceConfig, new ReqVo(sql.toString()), LsKyJssyb.class);
        List<LsKyJssyb> translateList = (List<LsKyJssyb>) dbList;
        if (!CollectionUtils.isEmpty(translateList)) {
            for (LsKyJssyb item : translateList) {
                if (StringUtils.isEmpty(item.getMc())) {
                    continue;
                }
                keyword = keyword.contains(item.getMc()) ? RemoveDuplicateCharacters.removeDuplicates(keyword, item.getMc()) : keyword;
            }
        }
        log.info("翻译工具 替换后 keyword = {}", keyword);
        String data = TranslateNewUtils.translateNotBqTitle(keyword, translateUrl);
        return AjaxResult.success(data);
    }

    public AjaxResult translateUtils(String keyword, String source, String target) {
        log.info("翻译工具 keyword = {}", keyword);
        CrudInterfaceUtils crudInterfaceUtils = new CrudInterfaceUtils();
        StringBuilder sql = new StringBuilder();
        sql.append("SELECT ID, MC, JG FROM J_KY_S_JSSYB ORDER BY ID ");
        Object dbList = crudInterfaceUtils.getList(dbInterfaceConfig, new ReqVo(sql.toString()), LsKyJssyb.class);
        List<LsKyJssyb> translateList = (List<LsKyJssyb>) dbList;
        if (!CollectionUtils.isEmpty(translateList)) {
            for (LsKyJssyb item : translateList) {
                if (StringUtils.isEmpty(item.getMc())) {
                    continue;
                }
                keyword = keyword.contains(item.getMc()) ? RemoveDuplicateCharacters.removeDuplicates(keyword, item.getMc()) : keyword;
            }
        }
        log.info("翻译工具 替换后 keyword = {}", keyword);
        String data = TranslateNewUtils.translateTypeByTitle(keyword, translateUrl, source, target);
        return AjaxResult.success(data);
    }

    public AjaxResult queryList(SearchVO svo) {
        IPage<LsKyJssyb> ipage = new Page<>();
        ipage.setCurrent(svo.getPageNum());
        ipage.setSize(svo.getPageSize());
        CrudInterfaceUtils crudInterfaceUtils = new CrudInterfaceUtils();
        StringBuilder totalSql = new StringBuilder();
        totalSql.append("SELECT COUNT(ID) FROM J_KY_S_JSSYB");
        Object totalObj = crudInterfaceUtils.getList(dbInterfaceConfig, new ReqVo(totalSql.toString()), Integer.class);
        Integer total = (Integer) totalObj;
        double result = Math.ceil((double) total / svo.getPageSize());
        int pages = (int) result;
        ipage.setPages(pages);
        ipage.setTotal(total);
        StringBuilder sql = new StringBuilder();
        sql.append("SELECT ID, MC, JG FROM J_KY_S_JSSYB ")
                .append(" where MC like '%")
                .append(svo.getName())
                .append("%' or JG like '%")
                .append(svo.getName())
                .append("%' ")
                .append("ORDER BY ID ")
                .append(" LIMIT ").append((svo.getPageNum() - 1) * svo.getPageSize())
                .append(", ").append(svo.getPageSize());
        Object dbList = crudInterfaceUtils.getList(dbInterfaceConfig, new ReqVo(sql.toString()), LsKyJssyb.class);
        List<LsKyJssyb> translateList = (List<LsKyJssyb>) dbList;
        ipage.setRecords(translateList);
        return AjaxResult.success(ipage);
    }

    public AjaxResult insert(LsKyJssyb translate) {
        CrudInterfaceUtils crudInterfaceUtils = new CrudInterfaceUtils();
        StringBuilder sql = new StringBuilder();
        sql.append("INSERT INTO  J_KY_S_JSSYB (ID, MC, JG) VALUES (")
                .append(IdUtils.generateId()).append(", '")
                .append(translate.getMc())
                .append("', '")
                .append(translate.getJg())
                .append("')");
        crudInterfaceUtils.create(dbInterfaceConfig, new ReqVo(sql.toString()));
        return AjaxResult.success();
    }

    public AjaxResult update(LsKyJssyb translate) {
        CrudInterfaceUtils crudInterfaceUtils = new CrudInterfaceUtils();
        StringBuilder sql = new StringBuilder();
        sql.append("UPDATE J_KY_S_JSSYB ")
                .append(" SET MC = ")
                .append(translate.getMc())
                .append(" , JG = ")
                .append(translate.getJg())
                .append(" WHERE ID = ")
                .append(translate.getId());
        crudInterfaceUtils.update(dbInterfaceConfig,new ReqVo(sql.toString()));
        return AjaxResult.success();
    }

    public AjaxResult batchDelete(String ids) {
        CrudInterfaceUtils crudInterfaceUtils = new CrudInterfaceUtils();
        StringBuilder sql = new StringBuilder();
        sql.append("DELETE FROM J_KY_S_JSSYB ")
                .append(" WHERE ID in (")
                .append(ids)
                .append(")");
        crudInterfaceUtils.delete(dbInterfaceConfig,new ReqVo(sql.toString()));
        return AjaxResult.success();
    }


}
